package com.study.common;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*代码生成插件*/
@Data
public class GeneratePlugin {
    String projectPath;
    String basePackage;
    //类路径
    String controllerPath = "controller";
    String servicePath = "service";
    String serviceImplPath = servicePath + "." + "impl";
    String mapperPath = "mapper";
    String xmlPath = "mapper";
    String entityPath = "model.entity";
    //其余类名默认的即可
    String serviceName = "%sService";
    String author = "Admin";
    String[] tablePrefix = new String[]{}, includeTable = new String[]{}, excludeTable = new String[]{};
    String url, username, password, driverName;
    Class<?> superServiceClass = IService.class;
    Class<?> superServiceImplClass = ServiceImpl.class;
    String[] superEntityColumns = new String[]{};
    //默认逻辑删除字段
    String logicDeleteFieldName = "delete_status";
    //默认自动填充配置
    List<TableFill> tableFillList = Stream.of(
            new TableFill("gmt_create", FieldFill.INSERT),
            new TableFill("gmt_modified", FieldFill.INSERT_UPDATE))
            .collect(Collectors.toList());
    //默认乐观锁字段
    private String versionFieldName = "version";

    public static GeneratePlugin build(
            String projectPath, String url, String userName, String password,
            String driverClassName, String[] tablePrefix, String[] includeTable) {
        GeneratePlugin generatePlugin = new GeneratePlugin();
        generatePlugin.setProjectPath(projectPath);
        generatePlugin.setUrl(url);
        generatePlugin.setUsername(userName);
        generatePlugin.setPassword(password);
        generatePlugin.setDriverName(driverClassName);
        generatePlugin.setTablePrefix(tablePrefix);
        generatePlugin.setIncludeTable(includeTable);
        return generatePlugin;
    }

    public void execute() {
        if (StringUtils.isAnyBlank(projectPath, basePackage, url, username, password, driverName)) {
            throw new NullPointerException("必要属性不能为空");
        }
        AutoGenerator generator = new AutoGenerator();
        //1、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(projectPath);
        globalConfig.setAuthor(author);
        globalConfig.setIdType(IdType.AUTO);
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setBaseResultMap(true);
        globalConfig.setBaseColumnList(true);
        globalConfig.setServiceName(serviceName);
        //实体属性 Swagger2 注解
        globalConfig.setSwagger2(true);
        generator.setGlobalConfig(globalConfig);
        //2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setUrl(url);
        dsc.setDriverName(driverName);
        generator.setDataSource(dsc);
        //3、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //rest请求
        strategy.setRestControllerStyle(true);
        //自动lombok；
        strategy.setEntityLombokModel(true);
        strategy.setTablePrefix(tablePrefix);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude(includeTable);
        strategy.setExclude(excludeTable);
        //逻辑删除---强烈不建议使用，具体参考说明文档
        //strategy.setLogicDeleteFieldName(logicDeleteFieldName);
        //自动填充配置
        strategy.setTableFillList(tableFillList);
        //乐观锁
        strategy.setVersionFieldName(versionFieldName);
        //父类相关
        strategy.setSuperServiceImplClass(superServiceImplClass);
        strategy.setSuperServiceClass(superServiceClass);
        strategy.setSuperEntityColumns(superEntityColumns);
        //不自动转下划线，采用驼峰
        strategy.setControllerMappingHyphenStyle(false);
        generator.setStrategy(strategy);
        //4、包的配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(basePackage);
        pc.setController(controllerPath);
        pc.setService(servicePath);
        pc.setServiceImpl(serviceImplPath);
        pc.setMapper(mapperPath);
        pc.setXml(xmlPath);
        pc.setEntity(entityPath);
        generator.setPackageInfo(pc);
        generator.execute();
    }

}
